package datastructure.book.dp._2_linear_pre_section.single.all;

public class _6_Solution {

    public int maxSubarraySumCircular(int[] nums) {
        int max = nums[0];
        int preMax = nums[0];
        int min = nums[0];
        int preMin = nums[0];
        int sum = nums[0];
        for (int i = 1; i < nums.length; i++) {
            preMax = Math.max(preMax+nums[i],nums[i]);
            preMin = Math.min(preMin+nums[i],nums[i]);
            max = Math.max(preMax,max);
            min = Math.min(preMin,min);
            sum+=nums[i];
        }
        if (max < 0) {
            return max;
        } else {
            return Math.max(max,sum-min);
        }
    }
}
